import { Entity, Column, OneToMany } from 'typeorm';
import { IsNotEmpty } from 'class-validator';
import { CommonEntity } from '@/common/entities/common.entity';
import { OrgImage } from '@/modules/orgImage/models/orgImage.entity';
import { Course } from '@/modules/course/models/course.entity';
import { Card } from '@/modules/card/models/card.entity';
import { Product } from '@/modules/product/models/product.entity';

@Entity('organization')
export class Organization extends CommonEntity {
  @Column({
    comment: '营业执照',
    default: '',
    nullable: true,
  })
  @IsNotEmpty()
  businessLicense: string;
  @Column({
    comment: '法人身份证正面',
    nullable: true,
  })
  identityCardFrontImg: string;
  @Column({
    comment: '法人身份证反面',
    nullable: true,
  })
  identityCardBackImg: string;
  @Column({
    type: 'text',
    comment: '标签 以, 隔开',
    nullable: true,
  })
  tags: string;
  @Column({
    type: 'text',
    comment: '简介',
    nullable: true,
  })
  description: string;
  @Column({
    comment: '机构名',
    nullable: true,
    default: '',
  })
  name: string;
  @Column({
    comment: 'logo',
    nullable: true,
  })
  logo: string;

  @Column({
    comment: '地址',
    nullable: true,
  })
  address: string;
  @Column({
    comment: '经度',
    nullable: true,
  })
  longitude: string;
  @Column({
    comment: '纬度',
    nullable: true,
  })
  latitude: string;
  @Column({
    comment: '电话',
    nullable: true,
  })
  tel: string;

  // 这里写上关联关系：一对多
  // {cascade:true}这个级联很重要，不然无法联表
  @OneToMany(() => OrgImage, (orgImage) => orgImage.orgIdForFront, {
    cascade: true,
  })
  orgFrontImg?: OrgImage[];
  @OneToMany(() => OrgImage, (orgImage) => orgImage.orgIdForRoom, {
    cascade: true,
  })
  orgRoomImg?: OrgImage[];
  @OneToMany(() => OrgImage, (orgImage) => orgImage.orgIdForOther, {
    cascade: true,
  })
  orgOtherImg?: OrgImage[];

  // 和课程的关系
  // {cascade:true}这个级联在课程内写了，这里就不要写，（原则，那边操作频繁，级联就放那边）
  @OneToMany(() => Course, (course) => course.org)
  courses?: Course[];

  @OneToMany(() => Card, (card) => card.org)
  cards?: Card[];

  @OneToMany(() => Product, (product) => product.org)
  products?: Product[];
}
